package com.qi.spzx.manager.controller;

import cn.hutool.http.server.HttpServerRequest;
import com.qi.spzx.manager.service.SysMenuService;
import com.qi.spzx.manager.service.SysUserService;
import com.qi.spzx.manager.service.ValidateCodeService;
import com.qi.spzx.model.dto.system.LoginDto;
import com.qi.spzx.model.entity.system.SysUser;
import com.qi.spzx.model.vo.common.Result;
import com.qi.spzx.model.vo.common.ResultCodeEnum;
import com.qi.spzx.model.vo.system.LoginVo;
import com.qi.spzx.model.vo.system.SysMenuVo;
import com.qi.spzx.model.vo.system.ValidateCodeVo;
import com.qi.spzx.utils.AuthContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "首页接口")
@RestController
@RequestMapping(value = "/admin/system/index")
public class IndexController {

    @Autowired
    private SysUserService sysUserService ;

    @Autowired
    private ValidateCodeService validateCodeService;

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 生成图片验证码
     * @return
     */
    @Operation(summary = "生成图片验证码")
    @GetMapping(value = "/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode() {
        ValidateCodeVo codeVo = validateCodeService.generateValidateCode();
        return Result.build(codeVo,ResultCodeEnum.SUCCESS);
    }

    /**
     * 获取当前登录用户信息
     * @return
     */
//    @Operation(summary = "获取当前登录用户信息")
//    @GetMapping(value = "/getUserInfo")
//    public Result getUserInfo(@RequestHeader(name = "token") String token) {
//        SysUser sysUser = sysUserService.getUserInfo(token);
//        return Result.build(sysUser,ResultCodeEnum.SUCCESS);
//    }

    @Operation(summary = "获取当前登录用户信息")
    @GetMapping(value = "/getUserInfo")
    public Result getUserInfo() {
        return Result.build(AuthContextUtil.get(),ResultCodeEnum.SUCCESS);
    }


    /**
     * 登录接口
     * @param loginDto
     * @return
     */
    @Operation(summary = "登录接口")
    @PostMapping(value = "/login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto) {
        LoginVo loginVo = sysUserService.login(loginDto) ;
        return Result.build(loginVo , ResultCodeEnum.SUCCESS) ;
    }

    /**
     * 退出登录接口
     * @param token
     * @return
     */
    @Operation(summary = "退出登录")
    @GetMapping(value = "/logout")
    public Result logout(@RequestHeader(name = "token") String token) {
        sysUserService.logout(token);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    /**
     * 获取菜单
     * @return
     */
    @GetMapping("/menus")
    public Result menus() {
        List<SysMenuVo> sysMenuVoList =  sysMenuService.findUserMenuList() ;
        return Result.build(sysMenuVoList , ResultCodeEnum.SUCCESS) ;
    }

}